using System;
using System.Xml;
using System.Xml.XPath;

namespace ADEVSO.Xml
{
    /// <summary>
    /// UNFINISHED
    /// </summary>
    public class XmlDoc
    {
        #region Variables

        private XmlDocument _XmlDoc;
        private XmlElement _XmlRoot;
        private String _XmlFileName;

        #endregion

        #region Contructors

        /// <summary>
        /// Creates New Xml
        /// </summary>
        /// <param name="filename"></param>
        public XmlDoc(String filename)
        {
            _XmlFileName = filename;
        }

        #endregion

        #region Properties

        public XmlDocument XmlDocument
        {
            get
            {
                return _XmlDoc;
            }
            set
            {
                _XmlDoc = value;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Load Xml File
        /// </summary>
        public void Load()
        {
            // Create the File if it doesn't exits
            if (System.IO.File.Exists(_XmlFileName))
            {
                XmlTextReader XmlReader = new XmlTextReader(_XmlFileName);
                _XmlDoc = new XmlDocument();
                if (XmlReader.HasValue)
                {
                    _XmlDoc.Load(XmlReader);
                    _XmlRoot = _XmlDoc.DocumentElement;
                    //XmlNodeList list = _XmlDoc.SelectNodes("/" + _XmlRoot.Name + "/apply");
                    //XmlNode XmlNode = list.Item(0);
                }
                XmlReader.Close();
            }
        }

        /// <summary>
        /// Save Changes on XML File
        /// </summary>
        public void Save(String path)
        {
            _XmlDoc.Save(path + "\\" + _XmlFileName);
        }

        /// <summary>
        /// Selects the first XmlNode that matches the XPath expression
        /// </summary>
        /// <param name="xpath">The XPath expression</param>
        /// <returns></returns>
        public XmlNode SelectNode(String xpath)
        {
            return _XmlDoc.SelectSingleNode(xpath);
        }

        /// <summary>
        /// Selects the first XmlNode that matches the XPath expression
        /// </summary>
        /// <param name="xpath">The XPath expression</param>
        /// <returns></returns>
        public XmlNode SelectNode(XmlNode parent, String xpath)
        {
            return parent.SelectSingleNode(xpath);
        }

        /// <summary>
        /// Selects a list of nodes matching the XPath expression
        /// </summary>
        /// <param name="xpath">The XPath expression</param>
        /// <returns></returns>
        public XmlNodeList SelectNodes(String xpath)
        {
            return _XmlDoc.SelectNodes(xpath);
        }

        /// <summary>
        /// Selects a list of nodes matching the XPath expression
        /// </summary>
        /// <param name="xpath">The XPath expression</param>
        /// <returns></returns>
        public XmlNodeList SelectNodes(XmlNode parent, String xpath)
        {
            return parent.SelectNodes(xpath);
        }

        /// <summary>
        /// Insert New Apply to Xml
        /// </summary>
        public void New()
        {
            // Create New Apply Node
            XmlElement nApply = _XmlDoc.CreateElement("apply");
            nApply.SetAttribute("prefix", "Mrs.");

            // Add New Contact Node
            nApply.AppendChild(NewContact());
            
            // Add Apply Node
            _XmlRoot.AppendChild(nApply);
        }

        private XmlNode NewContact()
        {
            XmlNode nContact = _XmlDoc.CreateNode(XmlNodeType.Element, "contact", null);

            XmlElement xmlElement = _XmlDoc.CreateElement("profession");
            xmlElement.InnerText = "Ing. en Sistemas";

            nContact.AppendChild(xmlElement);

            return nContact;
        }

        #endregion
    }
}
